package com.fmy.mcsecurity.starter;

import com.fmy.mcsecurity.config.McSecurityInitConfig;
import com.fmy.mcsecurity.integration.TypeDefultIntegration;
import com.fmy.mcsecurity.integration.TypeIntegration;
import com.fmy.mcsecurity.login.safety.violence.SafetyViolenceConfig;
import com.fmy.mcsecurity.login.service.DefultRefreshTokenService;
import com.fmy.mcsecurity.login.service.RefreshTokenService;
import com.fmy.mcsecurity.token.*;
import com.fmy.mcsecurity.token.impl.TokenVerificationDefult;
import com.fmy.mcsecurity.login.hander.*;
import com.fmy.mcsecurity.token.impl.UidTokenGenerate;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

public class StarterConfigution {


    @Bean
    public McSecurityInitConfig mcSecurityInitConfig() {
        return new McSecurityInitConfig();
    }

    @Bean
    @Primary
    public AuthenticationSuccessHandler authenticationSuccessHandler(TokenManager tokenManager, LoginHandler loginHandler) {
        return new AuthenticationSuccessHandlerImpl(loginHandler, tokenManager);
    }

    @Bean
    @Primary
    public AuthenticationFailureHandler authenticationFailureHandler(LoginHandler loginHandler) {
        return new AuthenticationFailureHandlerImpl(loginHandler);
    }

    @Bean
    public TokenManager mcSecurityTokenManager(TokenGenerate tokenGenerate, TokenStorage tokenStorage) {
        return new TokenManager(tokenGenerate, tokenStorage);
    }

    @Bean
    @ConditionalOnMissingBean
    public TokenVerification mcSecurityTokenVerification(TokenManager tokenManager) {
        return new TokenVerificationDefult(tokenManager);
    }

    @Bean
    @ConditionalOnMissingBean
    public RefreshTokenService mcSecurityRefreshTokenService() {
        return new DefultRefreshTokenService();
    }

    @Bean
    @ConditionalOnMissingBean
    public TokenVerfiyExceptionHander mcSecurityTokenExceptionHander() {
        return new TokenVerfiyExceptionHanderDefult();
    }

    @Bean
    @ConditionalOnMissingBean
    public TypeIntegration mcSecurityTypeDefultIntegration() {
        return new TypeDefultIntegration();
    }

    @Bean
    @ConditionalOnMissingBean
    public TokenGenerate mcSecurityTokenGenerate() {
        return new UidTokenGenerate();
    }
}
